package com.zyh.flink.day05;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.restartstrategy.RestartStrategies;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class RestartStrategyJob {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();

        environment.enableCheckpointing(5000);
        //失败重试次数3,间隔5s
        //environment.setRestartStrategy(RestartStrategies.fixedDelayRestart(3, Time.seconds(5)));
        //20s内允许失败2次第三次程序停止,间隔5s
        environment.setRestartStrategy(RestartStrategies.failureRateRestart(3,Time.seconds(20),Time.seconds(5)));

        DataStreamSource<String> hadoop10 = environment.socketTextStream("hadoop10", 9999);
        SingleOutputStreamOperator<Integer> result = hadoop10.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String s, Collector<String> collector) throws Exception {
                String[] ss = s.split("\\s+");
                for (String s1 : ss) {
                    collector.collect(s1);
                }
            }
        }).map(new MapFunction<String, Integer>() {
            @Override
            public Integer map(String s) throws Exception {
                return Integer.valueOf(s);
            }
        });

        result.print();

        environment.execute("Job");
    }
}
